<template>
  <div id="add">
    <el-dialog
      :title="obj.isadd ? '分类添加' : '分类修改'"
      :visible.sync="obj.istrue"
      width="50%"
      @close="cancel"
    >
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        style="width: 45%; background: #fff"
      >
        <el-form-item label="分类名称">
          <el-input v-model="form.title "></el-input>
        </el-form-item>
        <el-form-item label="上传图片">
          <div class="filebox">
            <h2>+</h2>
            <img :src="imgURL" alt="" v-if="imgURL" />
            <input type="file" @change="changeimg" />
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submitadd" v-if="obj.isadd"
          >确 定</el-button
        >
        <el-button type="primary" v-else @click="edit">修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { successAlert, warningAlert } from "../../../util/alert.js";
import { mapActions, mapGetters } from "vuex";
import { banneradd, bannerinfo, banneredit } from "../../../requset/api";
export default {
  props: ["obj"],
  data() {
    return {
      imgURL: "",
      form: {
        title : "",
        img: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      bannerlist: "banner/list",
    }),
  },
  methods: {
    ...mapActions({
      reqChangeBanner: "banner/reqChangeBanner",
    }),
    emty() {
      this.form = {
        title : "",
        img: "",
        status: 1,
      };
      this.imgURL = "";
    },
    cancel() {
      (this.obj.istrue = false), (this.obj.isadd = true), this.emty();
    },
    submitadd() {
      console.log(this.form);
      banneradd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqChangeBanner();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getone(id) {
      bannerinfo({ id: id }).then((res) => {
        this.form = res.data.list;
        this.imgURL = this.$pre + res.data.list.img;
        this.form.id = id;
      });
    },
    edit() {
      banneredit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqChangeBanner();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    changeimg(e) {
      let file = e.target.files[0];
      if (file.size >= 2 * 1024 * 1024) {
        warningAlert("图片超出规定的大小，请上传小于2M的图片");
      } else {
        this.imgURL = URL.createObjectURL(file);
        this.form.img = file;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.filebox {
  height: 200px;
  width: 200px;
  border: 1px dashed #cecece;
  position: relative;
  h2 {
    line-height: 200px;
    font-size: 30px;
    font-weight: bold;
    text-align: center;
  }
  input {
    height: 200px;
    width: 200px;
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
  }
  img {
    height: 200px;
    width: 200px;
    position: absolute;
    left: 0;
    top: 0;
  }
}
</style>